﻿
// ProjectAADlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "ProjectAA.h"
#include "ProjectAADlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//ImgCap *imgCap;
//ImgFile imgFile; 
//CTS CTSCam;

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

	// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum
	{
		IDD = IDD_ABOUTBOX
	};
#endif

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CProjectAADlg 对话框



CProjectAADlg::CProjectAADlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_PROJECTAA_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CProjectAADlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BUTTON_START, m_butStart);
	DDX_Control(pDX, IDC_BUTTON_STOP, m_butStop);
	DDX_Control(pDX, IDC_BUTTON_SAVE, m_butSave);
	DDX_Control(pDX, IDC_STATIC_20, m_static_20);
	DDX_Control(pDX, IDC_STATIC_42, m_static_42);
	DDX_Control(pDX, IDC_LIST_LOG2, m_listLog_Main);
}

BEGIN_MESSAGE_MAP(CProjectAADlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_32771, &CProjectAADlg::OnOpenLogDlg)
	ON_COMMAND(ID_32773, &CProjectAADlg::OnOpenMotorDlg)
	ON_COMMAND(ID_32784, &CProjectAADlg::OnOpenIODlg)
	ON_BN_CLICKED(IDC_BUTTON1, &CProjectAADlg::OnBnClickedButton1)
	ON_COMMAND(ID_32772, &CProjectAADlg::OnOpenProjectDlg)
	ON_BN_CLICKED(IDC_BUTTON_START, &CProjectAADlg::OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CProjectAADlg::OnBnClickedButtonStop)
	ON_COMMAND(ID_32776, &CProjectAADlg::OnOpenCaptureDlg)
	ON_COMMAND(ID_32777, &CProjectAADlg::OnOpenSFRDlg)
	ON_WM_SIZE()
	ON_MESSAGE(WM_SEND_SOCKET_SERVER_MSG_TO_MAIN_UI, &CProjectAADlg::OnReceiveServerMsg)	
	ON_WM_CTLCOLOR()
	ON_COMMAND(ID_32775, &CProjectAADlg::OnOpenAADlg)
	//	ON_WM_DRAWITEM()
	ON_BN_CLICKED(IDC_BUTTON_HOME_40, &CProjectAADlg::OnBnClickedButtonHome40)
	ON_COMMAND(ID_32781, &CProjectAADlg::OnOpenComDlg)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, &CProjectAADlg::OnBnClickedButtonSave)
	ON_BN_CLICKED(IDC_BUTTON_CLAER2, &CProjectAADlg::OnBnClickedButtonClaer2)
	ON_BN_CLICKED(IDC_BUTTON2, &CProjectAADlg::OnBnClickedButton2)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_SN, &CProjectAADlg::OnLvnItemchangedListSn)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST_SN, &CProjectAADlg::OnNMCustomdrawListSn)
	ON_COMMAND(ID_STD, &CProjectAADlg::OnOpenLightDlg)
	ON_COMMAND(ID_32785, &CProjectAADlg::OnOpenIICCheckDlg)
	ON_COMMAND(ID_32786, &CProjectAADlg::OnOpenControlDlg)
	ON_COMMAND(ID_32787, &CProjectAADlg::OnOpenPlcDlg)
	ON_MESSAGE(WM_SEND_LOG_MSG_TO_MAIN_UI_2, &CProjectAADlg::OnReceiveAndDisplaySystemLog)
	//ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST_LOG2, &CProjectAADlg::OnNMCustomdrawListLog)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_LOG2, &CProjectAADlg::OnNMDblclkListLog)
	ON_COMMAND(ID_32788, &CProjectAADlg::OnOpenLoginDlg)
	ON_BN_CLICKED(IDC_Clear_Msg, &CProjectAADlg::OnBnClickedClearMsg)
	ON_BN_CLICKED(IDC_Btn_Notauto, &CProjectAADlg::OnBnClickedBtnNotauto)
	ON_BN_CLICKED(IDC_Btn_Auto, &CProjectAADlg::OnBnClickedBtnAuto)
	ON_BN_CLICKED(IDC_Btn_NG, &CProjectAADlg::OnBnClickedBtnNg)
	ON_BN_CLICKED(IDC_BUTTON3, &CProjectAADlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &CProjectAADlg::OnBnClickedButton4)
END_MESSAGE_MAP()


// CProjectAADlg 消息处理程序

BOOL CProjectAADlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	unique_mutex = CreateMutex(NULL, FALSE, L"ZongmuAA");
	DWORD errCode = GetLastError();
	if (errCode == ERROR_ALREADY_EXISTS) {
		Sleep(500);
		CloseHandle(unique_mutex);
		MessageBox(L"AA软件已经被打开!", L"Warning", 0);
		::TerminateProcess(::GetCurrentProcess(), NULL);//终止原先程序
	}

	// 设置字体参数
	gSystemMgr.m_editFont.CreateFont(40, 0, 4, 0, FW_NORMAL, FALSE, FALSE, 0,
		ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_SWISS,
		_T("Arial"));
	m_static_20.SetFont(&gSystemMgr.m_editFont);
	m_static_20.SetWindowTextW(L"等待");
	m_static_42.SetFont(&gSystemMgr.m_editFont);
	m_static_42.SetWindowTextW(L"...");

	//轴的比率尺
	gSystemMgr.m_vCMotor[MOTOR_X].PositionRatio = 10000;//
	gSystemMgr.m_vCMotor[MOTOR_Y].PositionRatio = 10000;//
	gSystemMgr.m_vCMotor[MOTOR_Z].PositionRatio = 10000;//
	gSystemMgr.m_vCMotor[MOTOR_Xt].PositionRatio = 100000.0 / 38*0.8;
	gSystemMgr.m_vCMotor[MOTOR_Yt].PositionRatio = 5000.0 / 18 * 0.8;//改
	gSystemMgr.m_vCMotor[MOTOR_Zt].PositionRatio = 100000.0 / 28;//改
	gSystemMgr.m_vCMotor[Table_X].PositionRatio = 1000.0;//改
	gSystemMgr.m_vCMotor[Chart_Z].PositionRatio = 1000.0;//改


	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}

	}
	
	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	gSystemMgr.GeneralSettingIniInit();//读取型号配置

	m_menu.LoadMenuW(IDR_MENU1);
	this->SetMenu(&m_menu);
	//pSysMenu->EnableMenuItem(IDD_DIALOG_SFR, MF_GRAYED);

	//SetUiHandle();
	gSystemMgr.LoginConfigIniInit();//Login
	gSystemMgr.CaptureConfigIniInit();//采图配置
	gSystemMgr.AAConfigIniInit();//AA运动配置
	gSystemMgr.RS232ConfigIniInit();//RS232通讯配置
	gSystemMgr.StateConfigIniInit();//统计信息
	gSystemMgr.ReadErrorListIni();//报错信息
	gSystemMgr.LightConfigIniInit();//亮度测试
	gSystemMgr.IICCheckConfigIniInit();//版本号
	gSystemMgr.PLCConfigIniInit();//PLC
	USES_CONVERSION;

	gSystemMgr.m_ptrMainUI = this;

	if (m_cLogDlg == nullptr)
	{
		m_cLogDlg = new CLogDlg;
		m_cLogDlg->Create(IDD_DIALOG_LOG, this);
	}
	m_cLogDlg->ShowWindow(SW_HIDE);

	if (m_cIICCheckDlg == nullptr)
	{
		m_cIICCheckDlg = new CIICCheckDlg;
		m_cIICCheckDlg->Create(IDD_DIALOG_IIC_CHECK, this);
	}
	m_cIICCheckDlg->ShowWindow(SW_HIDE);

	bool bInit = gSystemMgr.PS400Init();//轴卡初始化
	if (bInit)
	{
		//gSystemMgr.MotorInit();//轴配置
		gSystemMgr.MotorReadConfig();

		Sleep(100);
	
		

		gSystemMgr.StartGetMotorIoThread();//状态？
		gSystemMgr.StartGetMotorEncoderThread();//位置？
	}
	
	switch (gSystemMgr.m_nImageSource)
	{
	case IMG_DEV_FILE:
		gSystemMgr.imgCap = (ImgCap *)&gSystemMgr.imgFile;
		
		break;
	case IMG_DEV_CTS:
		Cam_Init();
		gSystemMgr.m_bCTSInitFlag = 1;
		gSystemMgr.imgCap = (ImgCap *)&gSystemMgr.imgCTS;
		
		break;
	case IMG_RO_LONGO:
		gSystemMgr.imgCap = (ImgCap *)&gSystemMgr.imgRLG;
	case IMG_DEV_TSW:
		gSystemMgr.imgCap = (ImgCap *)&gSystemMgr.imgTSW;
		break;
	}
	gSystemMgr.changeImg.MakeSpaceTableITU();
	gSystemMgr.StartCaptureThread();
	gSystemMgr.StartImageTestThread();
	
	GetClientRect(&m_rcUiRect);

	if (!gSystemMgr.changeImg.init_lib())
	{
		gSystemLog.DisplayLogAndSave(L"convert init error", LV_ERROR);
	}

	gSystemMgr.StartAutoAAThread();
	gSystemMgr.Start_Check_img_exist_thread();
	gSystemMgr.StartScanButtonValueThread();
	gSystemMgr.StartOtherControlThread();
	gSystemMgr.StartPowerLinkThread();
	gSystemMgr.StartPLCThread();
	gSystemMgr.StartLightControlThread();
	
	//画按钮
	CRect rect;
	m_butStart.GetWindowRect(rect);
	long width = rect.right - rect.left;
	long height = rect.bottom - rect.top;
	HICON hicon_btn = (HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON_START), IMAGE_ICON, width - 30, height - 10, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION);
	m_butStart.SetIcon(hicon_btn);
	m_butStop.GetWindowRect(rect);
	width = rect.right - rect.left;
	height = rect.bottom - rect.top;
	hicon_btn = (HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON_STOP), IMAGE_ICON, width - 30, height - 10, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION);
	m_butStop.SetIcon(hicon_btn);
	m_butSave.GetWindowRect(rect);
	width = rect.right - rect.left;
	height = rect.bottom - rect.top;
	hicon_btn = (HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON_SAVE), IMAGE_ICON, width - 30, height - 10, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION);
	m_butSave.SetIcon(hicon_btn);

	//CRect rcScreen;
	//SystemParametersInfo(SPI_GETWORKAREA, 0, &rcScreen, 0);
	//gSystemMgr.m_nCrtScreenWidth = rcScreen.Width() / 1;
	//gSystemMgr.m_nCrtScreenHeight = rcScreen.Height() / 1;
	//::SetWindowPos(m_hWnd, HWND_TOP, gSystemMgr.m_nCrtScreenWidth * 0, gSystemMgr.m_nCrtScreenHeight * 0, gSystemMgr.m_nCrtScreenWidth, gSystemMgr.m_nCrtScreenHeight, SWP_SHOWWINDOW);

	LONG lStyle;
	m_listSN.SetBkColor(RGB(200, 220, 230));
	lStyle = GetWindowLong(m_listSN.m_hWnd, GWL_STYLE);
	lStyle &= ~LVS_TYPEMASK;
	lStyle |= LVS_REPORT;
	SetWindowLong(m_listSN.m_hWnd, GWL_STYLE, lStyle);
	m_listSN.ShowWindow(SW_SHOW);
	DWORD dwStyle = m_listSN.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;
	dwStyle |= LVS_EX_GRIDLINES;
	dwStyle |= LVS_EX_INFOTIP;

	m_listSN.SetExtendedStyle(dwStyle);
	m_listSN.SetFont(&gSystemMgr.m_logFont/*m_logFont*/);
	m_listSN.EnableWindow(TRUE);
	m_listSN.InsertColumn(0, L"产品过站条码", LVCFMT_LEFT, 300);
	m_listSN.Invalidate();
	m_listSN.ShowWindow(SW_SHOW);
	m_listSN.BringWindowToTop();


	m_listLog_Main.SetBkColor(RGB(200, 220, 250));
	//m_logList.SetBkColor(TRANSPARENT);
	//m_logList.SetTextColor(RGB(250, 250, 250));
	lStyle = GetWindowLong(m_listLog_Main.m_hWnd, GWL_STYLE);
	lStyle &= ~LVS_TYPEMASK;
	lStyle |= LVS_REPORT;
	SetWindowLong(m_listLog_Main.m_hWnd, GWL_STYLE, lStyle);
	m_listLog_Main.ShowWindow(SW_SHOW);
	dwStyle = m_listLog_Main.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;
	dwStyle |= LVS_EX_GRIDLINES;
	dwStyle |= LVS_EX_INFOTIP;

	m_listLog_Main.SetExtendedStyle(dwStyle);
	m_listLog_Main.SetFont(&gSystemMgr.m_logFont);
	m_listLog_Main.EnableWindow(TRUE);
	m_listLog_Main.InsertColumn(0, L"当前时刻", LVCFMT_LEFT, 90);
	m_listLog_Main.InsertColumn(1, L"日志详细描述", LVCFMT_LEFT, 2000);
	m_listLog_Main.ShowWindow(SW_HIDE);
	m_listLog_Main.EnableWindow(TRUE);
	m_listLog_Main.Invalidate();
	m_listLog_Main.ShowWindow(SW_SHOW);
	m_listLog_Main.BringWindowToTop();

	gSystemLog.m_hUiHandle_Main = this->GetSafeHwnd();
	//pSysMenu->EnableMenuItem(ID_32781, MF_GRAYED);

	SetWindowTextW(L"STK L1-S30 AA V1.0.6 [#Mes#R]");

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CProjectAADlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CProjectAADlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CRect rect;
		CPaintDC dc(this);
		GetClientRect(rect);
		dc.FillSolidRect(rect, RGB(169, 179, 195));//设置为绿色背景  

		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CProjectAADlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CProjectAADlg::SetUiHandle()
{
	m_menu.EnableMenuItem(ID_32781, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32773, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32778, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32782, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32776, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32777, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32775, MF_DISABLED);

}


void CProjectAADlg::OnOpenLogDlg()
{
	//if (gSystemMgr.loginflag == 0)return;
	if (m_cLogDlg != nullptr)
	{
		m_cLogDlg->ShowWindow(SW_SHOW);
	}
	//else
	//{
	//	m_cLogDlg = new CLogDlg;
	//	m_cLogDlg->Create(IDD_DIALOG_LOG, this);
	//}
}

void CProjectAADlg::OnOpenIODlg()
{
	if (m_cIODlg != nullptr)
	{
		delete m_cIODlg;
		m_cIODlg = nullptr;
	}
	if (m_cIODlg == nullptr)
	{
		m_cIODlg = new CIODlg;
		m_cIODlg->Create(IDD_DIALOG_IO, this);
	}
	m_cIODlg->ShowWindow(SW_SHOW);
}

void CProjectAADlg::OnOpenMotorDlg()
{
	//if (gSystemMgr.loginflag == 0)return;
	if (m_cMotorDlg != nullptr)
	{
		delete m_cMotorDlg;
		m_cMotorDlg = nullptr;
	}
	if (m_cMotorDlg == nullptr)
	{
		m_cMotorDlg = new CMotorDlg;
		m_cMotorDlg->Create(IDD_DIALOG_MOTOR, this);
	}
	m_cMotorDlg->ShowWindow(SW_SHOW);
}

void CProjectAADlg::OnBnClickedButton1()
{
	vector < vector<double>> vdSfrValue;
	vector<double> vdSfrValue1;
	vector<double> vdSfrValue2;
	vector<double> vdSfrValue3;
	vector<double> vdSfrValue4;
	vector<double> vdSfrValue5;

	double value[50][5] = {{7.79,3.31,30.17,16.04,14.78},{26.84,21.49,29.54,21.22,26.02},{33.29,25.82,34.25,25.78,31.73},{39.56,30.79,39.28,31.05,37.83},{46.01,36.20,44.48,35.72,42.23}, {52.23,42.22,49.23,40.17,47.14},{57.21,46.41,52.74,45.10,51.82},{62.52,50.74,56.13,49.60,55.70},{65.94,54.29,57.40,53.24,58.24},{68.29,57.10,58.63,56.03,59.45}, {69.20,58.89,58.75,57.42,60.54},{68.34,59.11,58.04,58.89,59.92},{65.56,58.53,55.55,58.88,57.88},{61.50,56.63,53.18,57.75,55.90},{56.35,53.92,49.72,55.12,52.38}, {49.37,50.14,45.81,52.15,48.44},{42.28,46.10,41.43,47.72,43.50},{34.46,41.82,37.01,43.71,38.86},{27.12,36.61,32.43,38.16,33.65},{19.31,31.43,28.44,33.40,29.17} };

	gSystemMgr.m_nAAStepCnt = 0;
	for (int z = 0; z < 20; z++)//50
	{
		for (int i = 0; i < SLANTED_BW_NUM; i++)
		{
			switch (i)
			{
			case 0:
				vdSfrValue1.push_back(value[z][0]);
				break;
			case 1:
				vdSfrValue2.push_back(value[z][1]);
				break;
			case 2:
				vdSfrValue3.push_back(value[z][2]);
				break;
			case 3:
				vdSfrValue4.push_back(value[z][3]);
				break;
			case 4:
				vdSfrValue5.push_back(value[z][4]);
				break;
			}
		}
		USES_CONVERSION;
		CString cstrLog;
		cstrLog.Format(L"sfr:%.2f,%.2f,%.2f,%.2f,%.2f", value[0][z], value[1][z], value[2][z], value[3][z], value[4][z]);
		gSystemLog.DisplayLogAndSave(cstrLog.GetBuffer(), LV_EVENT);
		gSystemMgr.m_nAAStepCnt++;
		vdSfrValue.clear();
		vdSfrValue.push_back(vdSfrValue1);
		vdSfrValue.push_back(vdSfrValue2);
		vdSfrValue.push_back(vdSfrValue3);
		vdSfrValue.push_back(vdSfrValue4);
		vdSfrValue.push_back(vdSfrValue5);
		gSystemMgr.DrawFocusCurve(vdSfrValue);
		Sleep(500);
	}
}


void CProjectAADlg::OnOpenProjectDlg()
{
	//if (gSystemMgr.loginflag == 0)return;
	if (m_cChangeProjectDlg != nullptr)
	{
		delete m_cChangeProjectDlg;
		m_cChangeProjectDlg = nullptr;
	}
	if (m_cChangeProjectDlg == nullptr)
	{
		m_cChangeProjectDlg = new CChangeProject;
		m_cChangeProjectDlg->Create(IDD_DIALOG_CHANGE_PROJECT, this);
	}
	m_cChangeProjectDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnBnClickedButtonStart()
{
	gSystemMgr.m_bStopCapture = 0;
	CWnd* pBtn = nullptr;
	pBtn = (CWnd*)GetDlgItem(IDC_BUTTON_START);
	pBtn->EnableWindow(FALSE);
}


void CProjectAADlg::OnBnClickedButtonStop()
{
	gSystemMgr.m_bStopCapture = 1;
	//gSystemMgr.rightpower = 0;
	gSystemMgr.imgCap->m_bInitFlag = FALSE;
	CWnd* pBtn = nullptr;
	pBtn = (CWnd*)GetDlgItem(IDC_BUTTON_START);
	pBtn->EnableWindow(TRUE);
}


void CProjectAADlg::OnOpenCaptureDlg()
{

	/*m_cCaptureDlg = new CImageCaptureDlg;
	m_cCaptureDlg->DoModal();*/

	//if (gSystemMgr.loginflag == 0)return;
	if (m_cCaptureDlg != nullptr)
	{
		delete m_cCaptureDlg;
		m_cCaptureDlg = nullptr;
	}
	if (m_cCaptureDlg == nullptr)
	{
		m_cCaptureDlg = new CImageCaptureDlg;
		m_cCaptureDlg->Create(IDD_DIALOG_CAPTURE, this);
	}
	m_cCaptureDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnOpenSFRDlg()
{
	//if (gSystemMgr.loginflag == 0)return;
	if (m_cSfrSettingDlg != nullptr)
	{
		delete m_cSfrSettingDlg;
		m_cSfrSettingDlg = nullptr;
	}
	if (m_cSfrSettingDlg == nullptr)
	{
		m_cSfrSettingDlg = new CSFRSettingDlg;
		m_cSfrSettingDlg->Create(IDD_DIALOG_SFR, this);
	}
	m_cSfrSettingDlg->ShowWindow(SW_SHOW);
}

void CProjectAADlg::Repaint(CWnd * pWnd, int last_Width, int now_Width, int last_Height, int now_Height, int cnt)
{
	if (pWnd)
	{
		CRect rect;
		CRect rect2;
		pWnd->GetWindowRect(&rect2);
		rect = OriRect[cnt];
		int n = 62;
		int m = 8;
		//rect2.top = rect2.top - n;
		//rect2.bottom = rect2.bottom - n;
		ScreenToClient(&rect2);

		double proportion_x = now_Width / (double)last_Width;
		double proportion_y = now_Height / (double)last_Height;

		rect.left = (long)(rect.left*proportion_x + 0.5 - m * proportion_x);
		rect.right = (long)(rect.left + RectW[cnt] * proportion_x + 0.5);
		rect.top = (long)(rect.top *proportion_y + 0.5 - n * proportion_y);
		rect.bottom = (long)(rect.top + RectH[cnt] * proportion_y + 0.5);
		pWnd->MoveWindow(&rect);
	}

}

void CProjectAADlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	if (nType == SIZE_RESTORED || nType == SIZE_MAXIMIZED)//窗体大小发生变动。处理程序
	{
		float fsp[2];
		POINT Newp; //获取现在对话框的大小
		CRect recta;
		GetClientRect(&recta); //取客户区大小   
		Newp.x = recta.right - recta.left;
		Newp.y = recta.bottom - recta.top;
		fsp[0] = (float)Newp.x / Old.x;
		fsp[1] = (float)Newp.y / Old.y;
		CRect Rect;
		int woc;
		CPoint OldTLPoint, TLPoint; //左上角
		CPoint OldBRPoint, BRPoint; //右下角
		HWND hwndChild = ::GetWindow(m_hWnd, GW_CHILD); //列出所有控件   
		while (hwndChild)
		{
			woc = ::GetDlgCtrlID(hwndChild);//取得ID
			GetDlgItem(woc)->GetWindowRect(Rect);
			ScreenToClient(Rect);
			OldTLPoint = Rect.TopLeft();
			TLPoint.x = long(OldTLPoint.x*fsp[0]);
			TLPoint.y = long(OldTLPoint.y*fsp[1]);
			OldBRPoint = Rect.BottomRight();
			BRPoint.x = long(OldBRPoint.x *fsp[0]);
			BRPoint.y = long(OldBRPoint.y *fsp[1]); //高度不可读的控件（如:combBox),不要改变此值.
			Rect.SetRect(TLPoint, BRPoint);
			GetDlgItem(woc)->MoveWindow(Rect, TRUE);
			hwndChild = ::GetWindow(hwndChild, GW_HWNDNEXT);
		}
		Old = Newp;
	}
	Invalidate();//更新窗口
	//if (0 == m_rcUiRect.left && 0 == m_rcUiRect.right && 0 == m_rcUiRect.top && 0 == m_rcUiRect.bottom)//第一次启动对话框时的大小变化不做处理
	//{
	//}
	//else
	//{
	//	if (0 == cx && 0 == cy)//如果是按下了最小化，则触发条件，这时不保存对话框数据
	//	{
	//		return;
	//	}
	//	CRect rectDlgChangeSize;
	//	GetClientRect(&rectDlgChangeSize);//存储对话框大小改变后对话框大小数据

	//	//if (rectDlgChangeSize.Width() > 400 && rectDlgChangeSize.Height() > 200) {
	//	CWnd *pWnd;
	//	int n = 0;
	//	n = n + 1; pWnd = GetDlgItem(IDC_BUTTON_SAVE); Repaint(pWnd, m_rcUiRect.Width(), rectDlgChangeSize.Width(), m_rcUiRect.Height(), rectDlgChangeSize.Height(), n);
	//	n = n + 1; pWnd = GetDlgItem(IDC_BUTTON_HOME_40); Repaint(pWnd, m_rcUiRect.Width(), rectDlgChangeSize.Width(), m_rcUiRect.Height(), rectDlgChangeSize.Height(), n);
	//	n = n + 1; pWnd = GetDlgItem(IDC_BUTTON_CLAER2); Repaint(pWnd, m_rcUiRect.Width(), rectDlgChangeSize.Width(), m_rcUiRect.Height(), rectDlgChangeSize.Height(), n);

	//}
	////GetClientRect(&m_rcUiRect); //save size of dialog
	//Invalidate();//更新窗口
}


void CProjectAADlg::OnCancel()
{
	gSystemMgr.m_bStopCapture = 1;
	gSystemMgr.imgCap->m_bInitFlag = FALSE;
	Sleep(200);
	KillTimer(TIMER_ID_IO_CONTROL_STATUS);
	ps400_stop_move_all(0, 0x07, STOP_SUDDEN);
	ps400_stop_move_all(1, 0x07, STOP_SUDDEN);
	gSystemMgr.m_nExitFlag = 1;
	delete m_cAADlg;
	//delete m_cAADlg;
	delete m_cLoginDlg;
	delete m_cMotorDlg;
	delete m_cLogDlg;
	delete m_cChangeProjectDlg;
	delete m_cCaptureDlg;
	delete m_cSfrSettingDlg;
	//delete m_cIODlg;
	delete m_cRS232Dlg;

	gSystemMgr.imgFile.release();
	gSystemMgr.imgCTS.release();
	gSystemMgr.imgRLG.release();

	gSystemMgr.m_cPower.com_close();
	::TerminateProcess(::GetCurrentProcess(), NULL);
	CDialogEx::OnCancel();
}

LRESULT CProjectAADlg::OnReceiveServerMsg(WPARAM wParam, LPARAM lParam)
{
	CString cstrp = (BSTR)wParam;
	SysFreeString((BSTR)wParam);

	gSystemLog.DisplayLogAndSave(cstrp.GetBuffer(), LV_EVENT);

	std::vector<wstring> vResultItem = _utils::SplitString(cstrp.GetBuffer(), L";");

	//gSystemLog.DisplayLogAndSave(cstrp.GetBuffer(), LV_EVENT);

	//在此处添加对接收数据的分析

	return LRESULT();
}


HBRUSH CProjectAADlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr1 = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

	DeleteObject(hbr);

	UINT id = pWnd->GetDlgCtrlID();
	CString Cstr;
	if (id == IDC_STATIC_2 || id == IDC_STATIC_50 || id == IDC_STATIC_51 || id == IDC_STATIC_54
		|| id == IDC_STATIC_56 || id == IDC_STATIC_58 || id == IDC_STATIC_60 || id == IDC_STATIC_62 || id == IDC_STATIC_64
		|| id == IDC_STATIC_66 || id == IDC_STATIC_68 || id == IDC_STATIC_69 || id == IDC_STATIC_71 || id == IDC_STATIC_72
		|| id == IDC_STATIC_74 || id == IDC_STATIC_76 || id == IDC_STATIC_85 || id == IDC_STATIC_87
		|| id == IDC_STATIC_84 || id == IDC_STATIC_86 || id == IDC_STATIC_53 || id == IDC_STATIC_92 || id == IDC_STATIC_93
		|| id == IDC_STATIC_96)
	{
		hbr = CreateSolidBrush(RGB(169, 179, 195));
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STEP0)
	{
		GetDlgItem(id)->GetWindowTextW(Cstr);
		if (Cstr == L"Testing")
		{
			hbr = CreateSolidBrush(RGB(222, 233, 20));
		}
		else if (Cstr == L"Pass")
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else if (Cstr == L"Fail")
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(70, 111, 166));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_42)
	{
		GetDlgItem(id)->GetWindowTextW(Cstr);
		if (Cstr == L"...")
		{
			hbr = CreateSolidBrush(RGB(157, 187, 210));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STEP1 || id == IDC_STEP2 || id == IDC_STEP3|| id == IDC_STEP4 || id == IDC_STEP5 || id == IDC_STEP6)
	{
		GetDlgItem(id)->GetWindowTextW(Cstr);
		if (Cstr == L"OK")
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else if (Cstr == L"NG")
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(222, 233, 20));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_47)
	{
		if (gSystemMgr.m_bHomeFlag)
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_48)
	{
		if (!gSystemMgr.m_bMotorErrorFlag)
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_49)
	{
		if (gSystemMgr.ConnectFlag_UVATA)
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_Notauto)
	{
		if (gSystemMgr.is_aa_open == false)
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_Auto)
	{
	if (gSystemMgr.is_aa_open)
	{
		hbr = CreateSolidBrush(RGB(0, 255, 0));
	}
	else
	{
		hbr = CreateSolidBrush(RGB(255, 0, 0));
	}
	pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_90)
	{
	if (gSystemMgr.rightpower == TRUE)
	{
		hbr = CreateSolidBrush(RGB(0, 255, 0));
	}
	else
	{
		hbr = CreateSolidBrush(RGB(255, 0, 0));
	}
	pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_91)
	{
	if (gSystemMgr.fpsTestResult == TRUE)
	{
		hbr = CreateSolidBrush(RGB(0, 255, 0));
	}
	else
	{
		hbr = CreateSolidBrush(RGB(255, 0, 0));
	}
	pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_44)
	{
		if (gSystemMgr.PLCConnectFlag)
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_46)
	{
	if (gSystemMgr.PLCConnectFlag_30)
	{
		hbr = CreateSolidBrush(RGB(0, 255, 0));
	}
	else
	{
		hbr = CreateSolidBrush(RGB(255, 0, 0));
	}
	pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_CX)
	{
		if (abs(gSystemMgr.m_dCenterXDif) > gSystemMgr.m_nSpecCenter)
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_CY)
	{
		if (abs(gSystemMgr.m_dCenterYDif) > gSystemMgr.m_nSpecCenter)
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_TX)
	{
		if (abs(gSystemMgr.TiltX) > gSystemMgr.m_dSpecAAAngle)
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_TY)
	{
		if (abs(gSystemMgr.TiltY) > gSystemMgr.m_dSpecAAAngle)
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_C)
	{
		if (gSystemMgr.SpecSfrFlag == 0) {
			if (gSystemMgr.SfrAvgValue[0] < gSystemMgr.m_dSpecSfr[0])
			{
				hbr = CreateSolidBrush(RGB(255, 0, 0));
			}
			else
			{
				hbr = CreateSolidBrush(RGB(0, 255, 0));
			}
		}
		else{
			if (gSystemMgr.SfrAvgValue[0] < gSystemMgr.m_dSpecSfr2[0])
			{
				hbr = CreateSolidBrush(RGB(255, 0, 0));
			}
			else
			{
				hbr = CreateSolidBrush(RGB(0, 255, 0));
			}
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_LU)
	{
		if (gSystemMgr.SpecSfrFlag == 0) {
			if (gSystemMgr.SfrAvgValue[1] < gSystemMgr.m_dSpecSfr[1])
			{
				hbr = CreateSolidBrush(RGB(255, 0, 0));
			}
			else
			{
				hbr = CreateSolidBrush(RGB(0, 255, 0));
			}
		}
		else {
			if (gSystemMgr.SfrAvgValue[1] < gSystemMgr.m_dSpecSfr2[1])
			{
				hbr = CreateSolidBrush(RGB(255, 0, 0));
			}
			else
			{
				hbr = CreateSolidBrush(RGB(0, 255, 0));
			}
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_RU)
	{
		if (gSystemMgr.SpecSfrFlag == 0) {
			if (gSystemMgr.SfrAvgValue[2] < gSystemMgr.m_dSpecSfr[1])
			{
				hbr = CreateSolidBrush(RGB(255, 0, 0));
			}
			else
			{
				hbr = CreateSolidBrush(RGB(0, 255, 0));
			}
		}
		else {
			if (gSystemMgr.SfrAvgValue[2] < gSystemMgr.m_dSpecSfr2[1])
			{
				hbr = CreateSolidBrush(RGB(255, 0, 0));
			}
			else
			{
				hbr = CreateSolidBrush(RGB(0, 255, 0));
			}
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_LD)
	{
		if (gSystemMgr.SpecSfrFlag == 0) {
			if (gSystemMgr.SfrAvgValue[3] < gSystemMgr.m_dSpecSfr[1])
			{
				hbr = CreateSolidBrush(RGB(255, 0, 0));
			}
			else
			{
				hbr = CreateSolidBrush(RGB(0, 255, 0));
			}
		}
		else {
			if (gSystemMgr.SfrAvgValue[3] < gSystemMgr.m_dSpecSfr2[1])
			{
				hbr = CreateSolidBrush(RGB(255, 0, 0));
			}
			else
			{
				hbr = CreateSolidBrush(RGB(0, 255, 0));
			}
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_RD)
	{
		if (gSystemMgr.SpecSfrFlag == 0) {
			if (gSystemMgr.SfrAvgValue[4] < gSystemMgr.m_dSpecSfr[1])
			{
				hbr = CreateSolidBrush(RGB(255, 0, 0));
			}
			else
			{
				hbr = CreateSolidBrush(RGB(0, 255, 0));
			}
		}
		else {
			if (gSystemMgr.SfrAvgValue[4] < gSystemMgr.m_dSpecSfr2[1])
			{
				hbr = CreateSolidBrush(RGB(255, 0, 0));
			}
			else
			{
				hbr = CreateSolidBrush(RGB(0, 255, 0));
			}
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else
	{
		hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	}
	hbr1 = hbr;
	return hbr1;
}

void CProjectAADlg::OnOpenAADlg()
{
	//if (gSystemMgr.loginflag == 0)return;
	if (m_cAADlg != nullptr)
	{
		delete m_cAADlg;
		m_cAADlg = nullptr;
	}
	if (m_cAADlg == nullptr)
	{
		m_cAADlg = new CAAControlDlg;
		m_cAADlg->Create(IDD_DIALOG_AA, this);
	}
	m_cAADlg->ShowWindow(SW_SHOW);
}

void CProjectAADlg::OnBnClickedButtonHome40()
{
	gSystemMgr.m_nButtonValue = 10;
}


void CProjectAADlg::OnOpenComDlg()
{
	//if (gSystemMgr.loginflag == 0)return;
	if (m_cRS232Dlg != nullptr)
	{
		delete m_cRS232Dlg;
		m_cRS232Dlg = nullptr;
	}
	if (m_cRS232Dlg == nullptr)
	{
		m_cRS232Dlg = new CRS232SetDlg;
		m_cRS232Dlg->Create(IDD_DIALOG_COM, this);
	}
	m_cRS232Dlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnBnClickedButtonSave()
{
	SYSTEMTIME st;
	GetLocalTime(&st);
	gSystemMgr.SaveTestReport(gSystemMgr.imgCap->bBmpTestBuf, gSystemMgr.imgCap->iBmpW, gSystemMgr.imgCap->iBmpH, 4);
	//gSystemMgr.SaveMes();
	gSystemMgr.m_nGetCodeFlag = 0;
	memset(gSystemMgr.SaveSN, 0, sizeof(gSystemMgr.SaveSN));
	USES_CONVERSION;
	SetDlgItemTextW(IDC_STATIC_SN4, A2T(gSystemMgr.SaveSN));
	SetDlgItemTextW(IDC_STATIC_SN5, A2T(gSystemMgr.SaveSN));
	gSystemMgr.m_nBarcodeLength = 0;
}


void CProjectAADlg::OnBnClickedButtonClaer2()
{
	if (IDYES == AfxMessageBox(L"确定清空 AA 站统计数据吗？", MB_YESNO))
	{
		CString cstr;
		cstr.Format(L"旧数据: %d,%d", gSystemMgr.m_nA_All, gSystemMgr.m_nA_NG);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR);
		gSystemMgr.m_nA_All = 0;
		gSystemMgr.m_nA_NG = 0;
		/*cstr.Format(L"B旧数据: %d,%d", gSystemMgr.m_nB_All, gSystemMgr.m_nB_NG);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR);
		gSystemMgr.m_nB_All = 0;
		gSystemMgr.m_nB_NG = 0;*/
		gSystemLog.DisplayLogAndSave(L"AA 站统计数据清除成功。", LV_ERROR);
	}
}



void CProjectAADlg::OnBnClickedButton2()
{
	gSystemMgr.m_nGetCodeFlag = 0;
	memset(gSystemMgr.SaveSN, 0, sizeof(gSystemMgr.SaveSN));
	USES_CONVERSION;
	SetDlgItemTextW(IDC_STATIC_SN4, A2T(gSystemMgr.SaveSN));
	SetDlgItemTextW(IDC_STATIC_SN5, A2T(gSystemMgr.SaveSN));
	gSystemMgr.m_nBarcodeLength = 0;
}



BOOL CProjectAADlg::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_SPACE) return TRUE; //如果是空格消息，返回；
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) return TRUE; //如果是Esc消息，返回；

	if (gSystemMgr.m_nGetCodeFlag == 0 && pMsg->message == WM_KEYDOWN) {
		USES_CONVERSION;
		//if (pMsg->wParam == VK_RETURN && gSystemMgr.m_nBarcodeLength > 0)
		//{
		//	gSystemLog.DisplayLogAndSave(A2T(gSystemMgr.SaveSN), LV_EVENT);
		//	SetDlgItemTextW(IDC_STATIC_SN, A2T(gSystemMgr.SaveSN));
		//	gSystemMgr.m_nGetCodeFlag = 1;
		//	return true;
		//}
		if (gSystemMgr.m_nBarcodeLength >= 50) {
			//gSystemMgr.m_nGetCodeFlag = -1;
			gSystemMgr.m_nBarcodeLength = 0;
			memset(gSystemMgr.SaveSN, 0, sizeof(gSystemMgr.SaveSN));
			return true;
		}
		switch (pMsg->wParam)
		{
		case VK_A:
		case VK_B:
		case VK_C:
		case VK_D:
		case VK_E:
		case VK_F:
		case VK_G:
		case VK_H:
		case VK_I:
		case VK_J:
		case VK_K:
		case VK_L:
		case VK_M:
		case VK_N:
		case VK_O:
		case VK_P:
		case VK_Q:
		case VK_R:
		case VK_S:
		case VK_T:
		case VK_U:
		case VK_V:
		case VK_W:
		case VK_X:
		case VK_Y:
		case VK_Z:
		case VK_0:
		case VK_1:
		case VK_2:
		case VK_3:
		case VK_4:
		case VK_5:
		case VK_6:
		case VK_7:
		case VK_8:
		case VK_9:
			gSystemMgr.SaveSN[gSystemMgr.m_nBarcodeLength] = pMsg->wParam;
			gSystemMgr.m_nBarcodeLength++;
			SetDlgItemTextW(IDC_STATIC_SN3, A2T(gSystemMgr.SaveSN));
			break;
		}
		//gSystemMgr.Barcode[gSystemMgr.m_nBarcodeLength] = pMsg->wParam;
		//gSystemMgr.m_nBarcodeLength++;
		if (pMsg->wParam == VK_RETURN && gSystemMgr.m_nBarcodeLength > 0)
		{
			if (gSystemMgr.m_nBarcodeLength == gSystemMgr.m_nSNCnt[1]) {
				strcpy(gSystemMgr.HolderSN, gSystemMgr.SaveSN);
				SetDlgItemTextW(IDC_STATIC_SN4, A2T(gSystemMgr.HolderSN));
				gSystemMgr.m_nBarcodeLength = 0;
				memset(gSystemMgr.SaveSN, 0, sizeof(gSystemMgr.SaveSN));
			}
			else if (gSystemMgr.m_nBarcodeLength == gSystemMgr.m_nSNCnt[0]) {
				strcpy(gSystemMgr.LensSN, gSystemMgr.SaveSN);
				SetDlgItemTextW(IDC_STATIC_SN5, A2T(gSystemMgr.LensSN));
				gSystemMgr.m_nBarcodeLength = 0;
				memset(gSystemMgr.SaveSN, 0, sizeof(gSystemMgr.SaveSN));
			}
		}
	}

	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN) return TRUE;

	return CDialogEx::PreTranslateMessage(pMsg);
}



void CProjectAADlg::OnLvnItemchangedListSn(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}


void CProjectAADlg::OnNMCustomdrawListSn(NMHDR *pNMHDR, LRESULT *pResult)
{
	NMLVCUSTOMDRAW* lplvcd = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
	int nIRow = int(lplvcd->nmcd.dwItemSpec);
	if (lplvcd->nmcd.dwDrawStage == CDDS_PREPAINT)
	{
		*pResult = CDRF_NOTIFYITEMDRAW;
		return;
	}
	if (lplvcd->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
	{
		lplvcd->clrTextBk = RGB(200, 220, 250);
		//lplvcd->clrTextBk =TRANSPARENT;
		*pResult = CDRF_NOTIFYSUBITEMDRAW;
		return;
	}
	else if (lplvcd->nmcd.dwDrawStage == (CDDS_SUBITEM | CDDS_PREPAINT | CDDS_ITEMPREPAINT))
	{
		COLORREF crText = RGB(10, 10, 10);
		if (lplvcd->iSubItem == 1)
		{
			crText = RGB(50, 50, 50);
			lplvcd->clrText = crText;
			*pResult = CDRF_DODEFAULT;
			return;
		}
	}
	*pResult = 0;
}


void CProjectAADlg::OnOpenLightDlg()
{
	//if (gSystemMgr.loginflag == 0)return;
	if (m_cLightTestDlg != nullptr)
	{
		delete m_cLightTestDlg;
		m_cLightTestDlg = nullptr;
	}
	if (m_cLightTestDlg == nullptr)
	{
		m_cLightTestDlg = new CLightTestDlg;
		m_cLightTestDlg->Create(IDD_DIALOG_STD, this);
	}
	m_cLightTestDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnOpenIICCheckDlg()
{
	//if (gSystemMgr.loginflag == 0)return;
	if (m_cIICCheckDlg == nullptr)
	{
		m_cIICCheckDlg = new CIICCheckDlg;
		m_cIICCheckDlg->Create(IDD_DIALOG_IIC_CHECK, this);
	}
	m_cIICCheckDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnOpenControlDlg()
{
	//if (gSystemMgr.loginflag == 0)return;
	if (m_cControlDlg != nullptr)
	{
		delete m_cControlDlg;
		m_cControlDlg = nullptr;
	}
	if (m_cControlDlg == nullptr)
	{
		m_cControlDlg = new CControlDlg;
		m_cControlDlg->Create(IDD_DIALOG_CONTROL, this);
	}
	m_cControlDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnOpenPlcDlg()
{
	//if (gSystemMgr.loginflag == 0)return;
	if (m_cPlcDlg != nullptr)
	{
		delete m_cPlcDlg;
		m_cPlcDlg = nullptr;
	}
	if (m_cPlcDlg == nullptr)
	{
		m_cPlcDlg = new CPlcDlg;
		m_cPlcDlg->Create(IDD_DIALOG_PLC, this);
	}
	m_cPlcDlg->ShowWindow(SW_SHOW);
}

LRESULT CProjectAADlg::OnReceiveAndDisplaySystemLog(WPARAM wParam, LPARAM lParam)
{
	CString cstrp = (BSTR)wParam;
	SysFreeString((BSTR)wParam);
	wstring wstrLogMsg = cstrp.GetString();
	std::vector<wstring> vLogItem = _utils::SplitString(wstrLogMsg, L"#");
	if (vLogItem.size() != 2)
	{
		return 0;
	}
	wstring wstrTime = vLogItem[0];
	wstring wstrMsg = vLogItem[1];
	int nIndexItem = m_listLog_Main.GetItemCount();
	m_listLog_Main.InsertItem(0, L"");
	m_listLog_Main.SetItemText(0, 0, wstrTime.c_str());
	m_listLog_Main.SetItemText(0, 1, wstrMsg.c_str());
	m_listLog_Main.Invalidate();
	if (nIndexItem >= 500)
	{
		m_listLog_Main.DeleteItem(500);
	}

	return 0;
}

void CProjectAADlg::OnNMDblclkListLog(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	m_listLog_Main.DeleteAllItems();
	*pResult = 0;
}

void CProjectAADlg::OnOpenLoginDlg()
{
	if (m_cLoginDlg != nullptr)
	{
		delete m_cLoginDlg;
		m_cLoginDlg = nullptr;
	}
	if (m_cLoginDlg == nullptr)
	{
		m_cLoginDlg = new LOGINDlg;
		m_cLoginDlg->Create(IDD_DIALOG_LOGIN, this);
	}
	m_cLoginDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnBnClickedClearMsg()
{
	// TODO: 在此添加控件通知处理程序代码
	gSystemMgr.m_cIo.SetOutputIo(15, OFF);
	gSystemMgr.m_nErrorCode = 0;
}


void CProjectAADlg::OnBnClickedBtnNotauto()
{
	// TODO: 在此添加控件通知处理程序代码
	gSystemMgr.is_aa_open = false;
	OnOpenControlDlg();
}


void CProjectAADlg::OnBnClickedBtnAuto()
{
	// TODO: 在此添加控件通知处理程序代码
	if (gSystemMgr.m_bHomeFlag)
	{
		gSystemMgr.is_aa_open = true;
		CString cstr;
		cstr.Format(L"开启自动模式,m_bHomeFlag:%d", gSystemMgr.m_bHomeFlag);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);	
	}	
	else {
		gSystemMgr.is_aa_open = false;		
		MessageBox(L"未回原，无法开启自动模式");
	}
}


void CProjectAADlg::OnBnClickedBtnNg()
{
	// TODO: 在此添加控件通知处理程序代码
	gSystemMgr.is_NGend = true;
	gSystemMgr.is_aa_open = true;
	gSystemLog.DisplayLogAndSave(L"11111111111111", LV_EVENT);
}


void CProjectAADlg::OnBnClickedButton3()
{
	gSystemMgr.is_NGend = 1;
	gSystemMgr.is_aa_open = true;
	gSystemLog.DisplayLogAndSave(L"22222222222222", LV_EVENT);
}


void CProjectAADlg::OnBnClickedButton4()
{
	gSystemMgr.is_NGend = 2;
	gSystemMgr.is_aa_open = true;
	gSystemLog.DisplayLogAndSave(L"33333333333333", LV_EVENT);
}
